package zjh_20230405;

import java.util.concurrent.Executor;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadFactory;

public class FixedPool {
    public static void main(String[] args) {
//        ExecutorService pool = Executors.newFixedThreadPool(5);
        MyThreadFactory myThreadFactory = new MyThreadFactory();
        ExecutorService pool = Executors.newFixedThreadPool(5, myThreadFactory);

        for (int i = 0; i < 10; i++) {
            pool.submit(new Runnable() {
                @Override
                public void run() {

                    System.out.println(Thread.currentThread().getName() + " is running");
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                }
            });
        }
        pool.shutdown();
//        System.out.println("main thread is end");
    }
}
class MyFixedThread implements ThreadFactory{

    int i;
    @Override
    public Thread newThread(Runnable r) {
        return new Thread("自定义线程" + i++);
    }
}
